package com.lenovo.codestatreport.git.controller;

import com.lenovo.codestatreport.git.model.GitJiraNameRelationship;
import com.lenovo.codestatreport.git.service.GitJiraNameRelationshipService;
import com.lenovo.utils.Utils;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * @author Howie Lu
 * @version Updated at 2019/06/04
 * @description git名、中文名、jira名的关系业务控制器
 */
@Controller
@RequestMapping("/staff")
public class GitJiraNameRelationshipController {
    @Autowired
    GitJiraNameRelationshipService gitJiraNameRelationshipService;

    Utils utils = new Utils();

    /**
     * 得到所有的对应关系
     *
     * @return 对应关系
     */
    @RequestMapping("/getAllRelations")
    @ResponseBody
    List<GitJiraNameRelationship> getAllRelations() {
        return gitJiraNameRelationshipService.getAllRelationships();
    }

    /**
     * 采用List<Map<String, Object>>的形式模拟json数据格式，返回的是json对象{gitname: xxx}的数组
     *
     * @param request http请求
     * @return json对象{gitname: xxx}的数组
     */
    @RequestMapping("/getAllStaff")
    @ResponseBody
    List<Map<String, Object>> getAllStaff(HttpServletRequest request) {
        String department = request.getParameter("department");
        List<Map<String, Object>> list = new ArrayList<>();
        String[] names = utils.readNameFromFile(department);
        for (String name : names) {
            Map<String, Object> map = new HashMap<>();
            map.put("gitname", name);
            list.add(map);
        }
        return list;
    }

    //弃用
    @RequestMapping("/getChineseNameByGitName")
    @ResponseBody
    Map<String, Object> getChineseNameByGitName(HttpServletRequest request) {
        List<String> names = gitJiraNameRelationshipService.getChineseNameByGitName(request.getParameter("gitName"));
        Map<String, Object> map = new HashMap<>();
        for (String name : names) {
            map.put("chineseName", name);
        }
        return map;
    }
}
